package hernanbosqued.parking;

import java.lang.ref.WeakReference;

import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

class HandlerMain extends Handler
{
	public static final int UPDATE_UI = 0xFEFE;
	public static final int ERROR     = 0xFFFF;

	private final WeakReference<ActivityMain> weak_reference;

	public HandlerMain( ActivityMain p_param )
	{
		weak_reference = new WeakReference( p_param );
	}

	@Override
	public void handleMessage( Message p_message )
	{
		ActivityMain activity = weak_reference.get( );
		
		switch ( p_message.what )
		{
			case UPDATE_UI:
				try
				{
					activity.adapter = new AdapterParkings( activity, p_message.obj );
					Location location = activity.location_manager.getLastKnownLocation( LocationManager.GPS_PROVIDER );

					activity.adapter.sortByDistance( location );
					
					ListView listview = (ListView) activity.findViewById( R.id.listview );
					listview.setAdapter( activity.adapter );
				}
				catch( Exception err )
				{
					Toast.makeText( activity, activity.getString( R.string.err_update ), Toast.LENGTH_SHORT ).show( );
				}
				break;
				
			case ERROR:
				Toast.makeText( activity, activity.getString( R.string.err_bridge ), Toast.LENGTH_SHORT ).show( );
				activity.progress_dialog.dismiss( );
				break;
		}
		
		activity.progress_dialog.dismiss( );
	}
}
